<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card
        >=={{ companyId }}=={{ showPermDialog }}
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog = true"
                >新增角色</el-button
              >
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="list">
              <el-table-column
                align="center"
                type="index"
                label="序号"
                width="120"
              >
                <template slot-scope="scoped">
                  <div>
                    {{ (page.page - 1) * page.pagesize + 1 + scoped.$index }}
                  </div>
                </template>
              </el-table-column>
              <el-table-column
                align="center"
                prop="name"
                label="角色名"
                width="240"
              />
              <el-table-column align="center" prop="description" label="描述" />
              <el-table-column align="center" label="操作">
                <template slot-scope="scoped">
                  <el-button
                    size="small"
                    type="success"
                    @click="assignPerm(scoped.row)"
                    >分配权限</el-button
                  >
                  <el-button
                    @click="editRole(scoped.row)"
                    size="small"
                    type="primary"
                    >编辑</el-button
                  >
                  <el-button
                    @click="deleteRole(scoped.row.id)"
                    size="small"
                    type="danger"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <!-- 放置分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height:60px"
            >
              <el-pagination
                :current-page="page.page"
                :page-size="page.pagesize"
                :total="page.total"
                layout="prev, pager, next"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input
                  v-model="formData.name"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="formData.companyAddress"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="formData.mailbox"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="formData.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width:400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 放置一个弹层组件 -->
    <!-- close事件 在点击确定的时候会触发 -->
    <el-dialog :title="ruleDia" :visible="showDialog" @close="btnCancel">
      <!-- =={{ roleForm }} -->
      <el-form
        ref="roleForm"
        :model="roleForm"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>

    <!-- 对话框 -->
    <el-dialog
      title="分配权限"
      :visible="showPermDialog"
      @close="btnPermCancel"
    >
      <!-- 权限是一颗树 -->
      <!-- 将数据绑定到组件上 -->
      <!-- check-strictly 如果为true 那表示父子勾选时  不互相关联 如果为false就互相关联 -->
      <!-- id作为唯一标识 -->

      <el-tree
        ref="permTree"
        :data="permData"
        :props="defaultProps"
        :show-checkbox="true"
        :check-strictly="true"
        :default-expand-all="true"
        :default-checked-keys="selectCheck"
        node-key="id"
      />
      <!-- 确定 取消 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnPermOK"
            >确定</el-button
          >
          <el-button size="small" @click="btnPermCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import {
  getRoleListApi,
  getCompanyInfoApi,
  deleteRoleApi,
  updateRoleApi,
  getRoleDetailApi,
  addRoleApi,
  assignPermApi
} from "@/api/setting";

import { getPermissionListApi } from "@/api/permission";

import { tranListToTreeData } from "@/utils";
export default {
  name: "",
  computed: {
    ...mapGetters(["companyId"]),
    ruleDia() {
      return this.roleForm.id ? "编辑角色" : "添加角色";
    }
  },
  data() {
    return {
      list: [], // 承接数组
      page: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 10,
        total: 0 // 记录总数
      },
      formData: {},
      showDialog: false,

      // 专门接收新增或者编辑的编辑的表单数据
      roleForm: {
        name: "",
        description: ""
      },
      rules: {
        name: [
          { required: true, message: "角色名称不能为空", trigger: "blur" }
        ],
        description: [
          { required: true, message: "角色描述不能为空", trigger: "blur" }
        ]
      },
      //  权限
      showPermDialog: false,
      permData: [],
      defaultProps: {
        label: "name"
      },
      selectCheck: [],
      roleId: null // 用来记录分配角色的id
    };
  },
  created() {
    this.getRoleList(); // 获取角色列表
    this.getCompanyInfo();
  },
  methods: {
    async getRoleList() {
      //console.log(rows, 4455);
      const {
        data: { total, rows }
      } = await getRoleListApi(this.page);
      this.page.total = total;
      this.list = rows;
    },
    changePage(newPage) {
      // newPage是当前点击的页码
      this.page.page = newPage; // 将当前页码赋值给当前的最新页码
      this.getRoleList();
    },
    //获取用户信息
    // 获取的公司的信息
    async getCompanyInfo() {
      setTimeout(async () => {
        const res = await getCompanyInfoApi(this.companyId);
        this.formData = res.data;
      }, 1000);
    },
    async deleteRole(id) {
      //  提示
      try {
        await this.$confirm("确认删除该角色吗");
        // 只有点击了确定 才能进入到下方
        await deleteRoleApi(id); // 调用删除接口
        this.getRoleList(); // 重新加载数据
        this.$message.success("删除角色成功");
      } catch (error) {
        console.log(error);
      }
    },
    // 编辑
    async editRole(row) {
      console.log(row, 334455);
      //this.roleForm = row;
      const res = await getRoleDetailApi(row.id);
      this.roleForm = res.data;
    },
    //  角色编辑+添加 取消
    btnCancel() {
      this.showDialog = false;
      this.$refs.roleForm.resetFields(); // 重置校验字段
      this.roleForm = {};
    },
    btnOK() {
      this.$refs.roleForm.validate(async valide => {
        if (valide) {
          if (this.roleForm.id) {
            // 编辑
            await updateRoleApi(this.roleForm);
          } else {
            // 新增
            await addRoleApi(this.roleForm);
          }
          // 重新拉取数据
          this.getRoleList();
          this.$message.success("操作成功");
          this.btnCancel();
        }
      });
    },
    //  分配权限
    btnPermCancel() {
      this.selectCheck = []; // 重置数据
      this.showPermDialog = false;
    },
    async btnPermOK() {
      //  给角色分配权限

      const res = await assignPermApi({
        permIds: this.$refs.permTree.getCheckedKeys(),
        id: this.roleId
      });
      console.log(res, 21);
      this.$message.success("分配权限成功");
      this.showPermDialog = false;
    },

    async assignPerm(row) {
      const res = await getPermissionListApi();
      console.log(row, 77);
      this.permData = tranListToTreeData(res.data, "0");
      this.roleId = row.id; //  记录角色的id
      //  根据角色id获取角色对应的权限
      const res2 = await getRoleDetailApi(this.roleId);
      console.log(res2.data.permIds, 888);
      this.selectCheck = res2.data.permIds;
      this.showPermDialog = true;
    }
  }
};
</script>
<style lang="less" scoped></style>
